/*<!-- js获取 url参数 -->*/

// 样例：getParValue(window.location.href, "msg");
function getParValue(str, key) {
    var num = str.indexOf("?");
    // 截取参数部分
    str = str.substr(num + 1); // 取得所有参数 stringvar.substr(start [, length ]

    var arr = str.split("&"); // 各个参数放到数组里
    console.log(arr);
    var tempPar;
    for (var i = 0; i < arr.length; i++) {
        tempPar = arr[i].split("=");
        if (tempPar[0] == key)
            return tempPar[1];
    }
    return null;
}

// 侧边栏Vue对象（包含每个不同页面的具体信息：modelName、三大表单的构造核心对象）

// Vue 对象
/*
* 0 抬头大字报（包含Title、modelName）
* 1 三大表单
* 1.1 queryForm
* 1.2 postForm
* 1.3 editForm
* 2 table
* */
// 0 抬头大字报（包含Title、modelName）
var bigTitle = new Vue({
    el: "#bigTitle",
    data: {
        titleName: "",
        modelName: ""
    },
    methods: {}
});
// 1.1 queryForm
var queryForm = new Vue({
    el: "#queryForm",
    data: {
        // 动态信息
        form: [],
        obj: new Object()
    },
    methods: {
        query: function () {
            // this.obj
            vm_table.queryList(this.obj, 1);
        }

    }
});
// 1.2 postForm
var postForm = new Vue({
    el: "#postForm",
    data: {
        postFormTitle: "",
        // 动态信息
        form: [],
        obj: new Object()
    },
    methods: {
        addObj: function () {
            // this.obj
            this.$http.post(bigTitle.modelName, this.obj).then(function (value) {
                alert("success.");
                vm_table.getAllByCurrentPage(1);
            }, function (reason) {
                alert(reason.body["msg"]);
            });
        }
    }
});
// 1.3 editForm
var editForm = new Vue({
    el: "#editForm",
    data: {
        editFormTitle: "",
        obj: new Object(),
        form: []
    },
    methods: {
        setObj: function (obj) {
            this.obj = obj;
        },
        updateObj: function () {
            // this.obj
            this.$http.put(bigTitle.modelName, this.obj).then(function (value) {
                alert("success.");
            }, function (reason) {
                alert(reason.body["msg"]);
            })
        }
    }
});
// 2 vm_table
var vm_table = new Vue({
    el: "#vm_table",
    data: {
        tableTitle: "",
        column: [],// 对属性列的抽象：vm_table.pageInfo.list[1]["id"]
        // 动态信息
        // 自己获取的动态信息
        pageInfo: new Object()
    },
    methods: {
        // 查询所有，通过页号
        getAllByCurrentPage: function (currentPage) {
            this.$http.get(bigTitle.modelName + "?currentPage=" + currentPage).then(function (value) {
                this.pageInfo = value.body;
            })
        },
        // 删除一个对象
        deleteOne: function (index) {
            this.$http.delete(bigTitle.modelName + "/" + this.pageInfo.list[index].id).then(function (value) {
                alert("success.");
                this.pageInfo.list.splice(index, 1);
            }, function (reason) {
                alert("失败：" + reason.body.msg)
            });
        },
        // 设置editeObj
        setEditeObj: function (obj) {
            editForm.setObj(obj)
        },
        // 条件分页查询
        queryList: function (obj, currentPage) {
            onstraint = new Object();
            onstraint["constraint"] = obj;
            onstraint["currentPage"] = currentPage;
            this.$http.post(bigTitle.modelName + "/eligible", onstraint).then(function (value) {
                this.pageInfo = value.body;
            }, function (reason) {
                alert("失败：" + reason.body["msg"]);
            });
        }
    }
});

// 侧边栏对象
var leftBar = new Vue({
    el: "#leftBar",
    data: {
        departmentManage: {
            titleName: "部门管理",
            modelName: "departmentList",
            tableTitle: "部门信息列表",
            column: [["编号", "id"], ["部门名称", "depname"]],
            postFormTitle: "新建部门信息",
            // 修改框信息
            editFormTitle: "修改部门信息",
            editForm: [{
                "labelName": "部门名称",
                "attributeName": "depname",
                "value": "",
                "type": "text",
                "other": null
            }]

        },
        post: {
            titleName: "职务管理",
            modelName: "postList",
            tableTitle: "职务信息列表",
            column: [["编号", "id"], ["职务名称", "postname"], ["所属部门", "depid"]],
            postFormTitle: "新建职务信息",
            editFormTitle: "修改职务信息",
            editForm: new Object()
        },
        staff: {
            titleName: "员工管理",
            modelName: "staffList",
            tableTitle: "员工信息列表",
            column: [["编号", "staffcode"],
                ["员工名称", "staffname"],
                ["生日", "birthday"],
                ["性别", "gender"],
                ["入职日期", "ondutydate"],
                ["职务", "postid"]
            ],
            postFormTitle: "建员工信息",
            editFormTitle: "修改员工信息",
            editForm: new Object()

        },
        lessontype: {
            titleName: "课程管理",
            modelName: "lessontypeList",
            tableTitle: "课程信息列表",
            column: [["编号", "id"],
                ["课程名称", "lessonname"],
                ["课程费用", "lessoncost"],
                ["总课时", "totaltime"],
                ["课程介绍", "remark"]
            ],
            postFormTitle: "新建课程信息",
            editFormTitle: "修改课程信息",
            editForm: new Object()
        },
        refer: {
            titleName: "咨询学生管理",
            modelName: "referList",
            tableTitle: "咨询信息列表",
            column: [["编号", "id"],
                ["咨询来源", "source"],
                ["咨询师", "userid"],
                ["学生姓名", "username"],
                ["状态", "rstate"],
                ["资源级别", "resourcelevel"]
            ],
            postFormTitle: "新建咨询信息",
            editFormTitle: "修改咨询信息",
            editForm: new Object()
        },
        student: {
            titleName: "在校学生管理",
            modelName: "studentList",
            tableTitle: "学生信息列表",
            column: [["编号", "id"],
                ["学生姓名", "stuname"],
                ["性别", "stusex"],
                ["入学时间", "begintime"],
                ["就业时间", "jobtime"],
                ["学生状态", "stustate"]
            ],
            postFormTitle: "新建学生信息",
            editFormTitle: "修改学生信息",
            editForm: new Object()
        },
        jobCompany: {
            titleName: "就业公司管理",
            modelName: "jobCompanyList",
            tableTitle: "就业公司列表",
            column: [["编号", "id"],
                ["工作单位名字", "companyname"],
                ["组织机构代码", "comcode"],
                ["单位地址", "address"],
                ["单位联系人", "linkman"],
                ["单位联系电话号码", "telephone"],
                ["工作岗位", "quarters"],
                ["薪酬", "emolument"],
                ["公司状态", "state"],
                ["备注", "remark"]
            ],
            postFormTitle: "新建就业公司",
            editFormTitle: "修改就业公司信息",
            editForm: new Object()
        },
        classManage: {
            titleName: "班级管理",
            modelName: "classList",
            tableTitle: "班级信息列表",
            column: [["编号", "id"],
                ["班级名称", "classname"],
                ["开授课程", "lessontypeid"],
                ["开班时间", "begintime"],
                ["毕业时间", "endtime"],
                ["状态", "cstate"],
                ["学生总数", "totalcount"],
                ["升学数", "gocount"],
                ["留班数", "leavecount"]
            ],
            postFormTitle: "新建班级信息",
            editFormTitle: "修改班级信息",
            editForm: new Object()
        },
        job: {
            titleName: "就业管理",
            modelName: "jobList",
            tableTitle: "就业信息列表",
            column: [["编号", "id"],
                ["学员姓名", "stuname"],
                ["性别", "gender"],
                ["班级", "classid"],
                ["学生状态", "jobstate"],
                ["工作公司", "jobcomid"]
            ],
            postFormTitle: "新建就业信息",
            editFormTitle: "修改就业信息",
            editForm: new Object()
        }
    },
    methods: {
        onclick: function (o, event) {
            // 静态信息刷新
            queryForm.obj = new Object();
            bigTitle.modelName = o.modelName;
            bigTitle.titleName = o.titleName;
            vm_table.tableTitle = o.tableTitle;
            vm_table.column = o.column;
            postForm.postFormTitle = o.postFormTitle;
            postForm.obj = new Object();
            // 修改框
            editForm.editFormTitle = o.editFormTitle;
            // 动态信息刷新，由于是多线程，所有这个放到第一个 应该会加快速度
            this.refresh(o);
            // 其他动态信息刷新
            vm_table.getAllByCurrentPage(1);
        },
        refresh: function (o) {
            // 更新主页面区的显示内容
            this.$http.get(o.modelName + "/form/query").then(function (value) {
                var t = value.body;
                console.log(t.inputItems);
                // 跟新queryForm
                queryForm.form = t.inputItems;
            }, function (reason) {
                console.log(reason.body);
            });
            this.$http.get(o.modelName + "/form/post").then(function (value) {
                var t = value.body;
                console.log(t.inputItems);
                // 跟新queryForm
                postForm.form = t.inputItems;
                editForm.form = t.inputItems;
            }, function (reason) {
                console.log(reason.body);
            });
            // 更新其他
        }
    }
});

// ------------ 初始化区
var model = getParValue(window.location.href, "model")
if (model != null)
    leftBar.onclick(leftBar[model]);
else
    leftBar.onclick(leftBar.departmentManage);